<?php


class Diet extends CI_Model{
		
	
	public  function new_diet(){
		
		$id_patient = $this->input->post('id_patient');
		$food_el =  $this->input->post('food_el');

		$this->db->update('diet', array('active' => 0), array('id_patient' => $id_patient, 'active'=>1)); 
		
		$this->db->insert('diet', array('id_patient' => $id_patient,'active' => 1)); 
		$id_diet = $this->db->insert_id();
		
		foreach($food_el as $food){
			$data = array(
				'id_diet' => $id_diet ,
				'id_food' => $food['id_food'] ,
				'id_time' => $food['id_time'] ,
				'amount' => $food['amount'] 
			);
			
			$this->db->insert('food_element', $data); 
		}
		
	}
	
	public function get_diet_by_patient($id_patient){
		$query = $this->db->get_where('diet',array('id_patient' => $id_patient, 'active' => 1));
		$diet = $query->row_array();
		return $diet['id'];
	}
		
		
}


?>
